package org.wlgzs.shop.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.wlgzs.shop.bean.GoodsAllKind;
import org.wlgzs.shop.bean.GoodsAllKindTwo;
import org.wlgzs.shop.bean.GoodsAllKindTwoExample;
import org.wlgzs.shop.bean.Msg;
import org.wlgzs.shop.dao.GoodsAllKindMapper;
import org.wlgzs.shop.dao.GoodsAllKindTwoMapper;
import org.wlgzs.shop.dao.StoreMapper;
import org.wlgzs.shop.service.IManagerKind;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class ManagerKInd implements IManagerKind {
    @Resource
    GoodsAllKindTwoMapper goodsAllKindTwoMapper;

    @Resource
    GoodsAllKindMapper goodsAllKindMapper;

    @Autowired
    IManagerKind managerKind;

    @Autowired
    StoreMapper storeMapper;

    /**
     *
     * @return
     */
    public List<GoodsAllKind> kindOne() {
        return goodsAllKindMapper.selectByExample(null);
    }

    /**
     *
     * @return
     */
    public List<GoodsAllKindTwo> KindTwo(){
        return goodsAllKindTwoMapper.selectByExample(null);
    }


    @Override
    //添加一级分类
    public Msg addOneKind(String oneKindDate) {
    int a=goodsAllKindMapper.insert(new GoodsAllKind(oneKindDate));
    if(a>0){
        return Msg.success().add("addOneKind","添加一级分类成功");
    }
    else {
        return Msg.fail().add("addOneKind","添加一级分类失败");
        }
    }

    //添加二级分类
    @Override
    public Msg addTwoKind(String twoKindDate) {
        int a=goodsAllKindTwoMapper.insert(new GoodsAllKindTwo(twoKindDate));
        if(a>0){
            return Msg.success().add("addOneKind","添加二级分类成功");
        }
        else {
            return Msg.fail().add("addOneKind","添加二级分类失败");
        }
    }

    //一级分类所有字段端
    @Override
    public List oneKind() {
        List<GoodsAllKind> all=kindOne();
        for(GoodsAllKind goodsAllKind:all){
            goodsAllKind.setList(managerKind.oneContainsTwo(goodsAllKind.getId()+""));
        }
        return all;
    }

    //二级分类的所有字段
    @Override
    public List twoKind() {
        List<GoodsAllKindTwo> all=KindTwo();
        return all;
    }

    //一级分类字段含有的二级分类字段
    @Override
    public List oneContainsTwo(String OneKind) {
        GoodsAllKindTwoExample goodsAllKindTwoExample=new GoodsAllKindTwoExample();
        GoodsAllKindTwoExample.Criteria criteria=goodsAllKindTwoExample.createCriteria();
        criteria.andGoodsKindIdEqualTo(OneKind);
        List<GoodsAllKindTwo> all=goodsAllKindTwoMapper.selectByExample(goodsAllKindTwoExample);
        return all;
    }

    //二级id获取一级id
    @Override
    public String selectTwoKind(String twoKindId) {
        int twoId=Integer.parseInt(twoKindId);
        return goodsAllKindTwoMapper.selectByPrimaryKey(twoId).getGoodsKindId();
    }

    //根据的storeId查询一级分类id
    @Override
    public String selectStr(String kindStoreId) {
        int storeId=Integer.parseInt(kindStoreId);
        return storeMapper.selectByPrimaryKey(storeId).getStoreKind();
    }

}
